// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import O1HD from "../../../engines/gameEngine";
import o1DrawInterface from "./o1DrawInterface";
import drawDetail from "./drawDetail";

const {ccclass, property} = cc._decorator;

@ccclass
export default class drawRecord extends cc.Component {

    @property(cc.Prefab)
    drawDetail: cc.Prefab = null;
    @property(cc.Node)
    svContentList: cc.Node = null;

    onLoad() {
        O1HD.off('drawRecord.OnEditDraw');
        O1HD.off('drawRecord.OnDeleteDraw');
        O1HD.on('drawRecord.OnEditDraw', this.OnEditDraw.bind(this));
        O1HD.on('drawRecord.OnDeleteDraw', this.OnDeleteDraw.bind(this));
    }

    private m_drawClass: o1DrawInterface = null;

    start() {

    }

    bindDrawInterface(drawClass: o1DrawInterface) {
        this.m_drawClass = drawClass;
        this.OnLoadDrawList();
    }

    protected OnEditDraw(nameKey: string) {
        O1HD.emit('o1DrawBoard.OnEditDraw', nameKey);
        this.OnBtnExit();
    }

    protected OnDeleteDraw(nameKey: string) {
        O1HD.getModules().uiControlModule.showDialogBox(`删除文件`, `删除后不可恢复,是否删除<${nameKey}>?`, true, isOk => {
            if (isOk) {
                O1HD.emit('o1DrawBoard.OnDeleteDraw', nameKey);
                //刷新
                this.svContentList.destroyAllChildren();
                this.scheduleOnce(this.OnLoadDrawList.bind(this));
                //
                // this.OnLoadDrawList();
            }
        }, this.node, '删除', '取消');
    }

    private OnLoadDrawList() {
        O1HD.getModules().uiControlModule.showLoading(this.node, '正在加载画册');
        //
        const dataInfo = this.m_drawClass.getAllData();
        for (let k in dataInfo) {
            let data = dataInfo[k];
            //
            let node = cc.instantiate(this.drawDetail);
            this.svContentList.addChild(node);
            let com = node.getComponent(drawDetail);
            com.setInfo(data);
        }
        O1HD.getModules().uiControlModule.hideLoading();
    }

    OnBtnExit() {
        this.svContentList.destroyAllChildren();
        this.node.destroy();
    }

    // update (dt) {}
}
